package com.sgl.myview.view;

import android.content.Context;
import android.graphics.Point;
import android.support.v4.widget.ViewDragHelper;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.view.MotionEvent;
import android.view.View;
import android.view.WindowManager;
import android.widget.LinearLayout;

/**
 * Created by XH on 2016/12/2.
 */
public class QQCeHuaLayout extends LinearLayout {
    private ViewDragHelper DragHelper;
    private Point mOriginalPosition = new Point();
    private Point mMovePosition = new Point();
    private LinearLayout mLeftView;
    private View mRightView;

    /**
     * 屏幕的高度
     */
    private int mScreenWidth;

    private int mDownY, mUpY;

    public QQCeHuaLayout(Context context, AttributeSet attrs) {
        super(context, attrs);
        /**
         * 获得屏幕的高度
         */
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        DisplayMetrics outMetrics = new DisplayMetrics();
        wm.getDefaultDisplay().getMetrics(outMetrics);
        mScreenWidth = outMetrics.widthPixels;
        initViewDragHelper();
    }


    @Override
    public boolean onInterceptTouchEvent(MotionEvent event) {
        return DragHelper.shouldInterceptTouchEvent(event);
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        DragHelper.processTouchEvent(event);
        return true;
    }

    @Override
    public void computeScroll() {
        if (DragHelper.continueSettling(true)) {
            invalidate();
        }
    }

    @Override
    protected void onLayout(boolean changed, int l, int t, int r, int b) {
        super.onLayout(changed, l, t, r, b);
        MarginLayoutParams lp = (MarginLayoutParams) getLayoutParams();
        lp.width = mScreenWidth + mLeftView.getChildAt(1).getWidth();
        setLayoutParams(lp);
        mOriginalPosition.x = mLeftView.getLeft();
        mOriginalPosition.y = mLeftView.getTop();
        mMovePosition.x = -mRightView.getWidth();
        mMovePosition.y = mLeftView.getTop();
        mRightView.setTranslationX(-mRightView.getWidth());
    }

    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mLeftView = (LinearLayout) getChildAt(0);
        mRightView = mLeftView.getChildAt(1);
    }

    void initViewDragHelper() {

        DragHelper = ViewDragHelper.create(this, 1.0f, new ViewDragHelper.Callback() {
            @Override
            public boolean tryCaptureView(View child, int pointerId) {
                return true;
            }

            @Override
            public int clampViewPositionHorizontal(View child, int left, int dx) {
                //left : 控件左上角坐标的 x 坐标
                if (child == mLeftView) {
                    if (left > 0) {//要向右移动
                        return 0;
                    } else if (left < mMovePosition.x) {
                        return mMovePosition.x;
                    } else {
                        return left;
                    }
                }
                return left;
            }

            @Override
            public int clampViewPositionVertical(View child, int top, int dy) {
                return mOriginalPosition.y;
            }


            //手指释放的时候回调
            @Override
            public void onViewReleased(View releasedChild, float xvel, float yvel) {
                if (releasedChild == mLeftView) {
                    if (xvel > -100) {
                        DragHelper.settleCapturedViewAt(mOriginalPosition.x, mOriginalPosition.y);
                    } else {
                        DragHelper.settleCapturedViewAt(mMovePosition.x, mMovePosition.y);
                    }
                    invalidate();
                }
            }

            @Override
            public void onViewPositionChanged(View changedView, int left, int top, int dx, int dy) {

            }

        });
    }

    public void toOriginal(){
        DragHelper.smoothSlideViewTo(mLeftView,mOriginalPosition.x, mOriginalPosition.y);
        //DragHelper.settleCapturedViewAt(mOriginalPosition.x, mOriginalPosition.y);
    }

}
